//Darius Smith
//DA3
//Using serial communication an LM34 to record temperature

#include <avr/io.h>
#include <stdio.h>
#include <avr/interrupt.h>

void printStringToScreen(char *sentence)
	{
    while ((*sentence !='\0')) // While the end of the sentence hasn't been reached
		{   // the output string will be displayed, character by character.
        while(!(UCSR0A&(1<<UDRE0)));
			UDR0 = *sentence;
		sentence++; // The position of next character of the output string is gotten.
		}
	}

int main(void)
	{
    DDRD	= 0xFF;				//enables output instead of input for the entire port D
    DDRC	= 0;				//leaves all port C ports directed towards input
    ADCSRA	= 0b10000010;		//(1000 0010)
    ADMUX	= 0x00;				//ref1 and ref0 use Vref and ADC0 will act as input.
    UCSR0B	= 0x08;				//third bit set (0100). Enables transmition
    UCSR0C	= 0x06;				//UCSR01 and UCSR00 (0110)
    UBRR0	= 0x33;				// With a baud rate of 9600, the value of UBRR0 will be, 8000000/(16*9600) - 1 = 51.083 = 51 = 0x33.
    TCCR1B	= 0x0C;				//(1100)sets the prescaler to 256
    OCR1A	= 0x7A12;			//(8000000 * 1)/256 = 7812.5, 31,250
    TIMSK1	|= (1 << OCIE1A);	//when TCNT1A reaches 31250ms, the interrupt function is called
    sei();						//enables interrupts
   char outputString[999];		//holds the value for the string to print to screen  
	
    while(1)
		{ //nothing happens
		}
	}

ISR(TIMER1_COMPA_vect)
	{
    char outputString[999]; //message to print on screen
    int temp;				//temperature value read from adc

    ADCSRA |= (1<<ADSC);
    while((ADCSRA&(1<<ADIF))==0);
                                                          
    temp = ADC;			
    snprintf(outputString, sizeof(outputString), "Temperature = %3d F\n\r", temp);
    printStringToScreen(outputString);
	}

